<?php
/**
 * Created by PhpStorm.
 * User: Egg
 * Date: 2018/3/30
 * Time: 11:59
 */

namespace Admin\Model;


use Think\Auth;

class MenuLogic
{
    /**
     * 从数据库拉取菜单
     * 若缓存(F)中存在，则从缓存中拉取
     * @param $uid  用户id
     * @return array|mixed
     */
    public function get_menus($uid)
    {
        $menu = F('menu');
        if (!empty($menu)) {
            return $menu;
        }
        $auth = new Auth();
        $u_groups = $auth->getGroups($uid);
        if (empty($u_groups)) {
            $this->error();
            exit;
        }
        $rules = array();
        foreach ($u_groups as $group) {
            $rules = array_merge($rules, explode(',', $group['rules']));
        }
        $rules = array_unique($rules);

        $where['id'] = array('IN', $rules);
        $where['use'] = 0;
        $where['status'] = 1;
        $result = D('Admin/Menu')
            ->where($where)
            ->select();
        $menu = array();
        foreach ($result as $key => $value) {
            $menu[$key]['title'] = $value['title'];
            $menu[$key]['url'] = '../'.$value['name'];
        }
        return $menu;
    }
}